package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Materialaddress;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.*;

@Repository("MaterialaddressDAO")
@Transactional
public class MaterialaddressDAOImpl extends AbstractJpaDao<Materialaddress> implements MaterialaddressDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Materialaddress.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public MaterialaddressDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Materialaddress findMaterialaddressById(Integer id) throws DataAccessException {

		return findMaterialaddressById(id, -1, -1);
	}

	@Transactional
	public Materialaddress findMaterialaddressById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findMaterialaddressById", startResult, maxRows, id);
			return (Materialaddress) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Materialaddress> findMaterialaddressByName(String name) throws DataAccessException {

		return findMaterialaddressByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Materialaddress> findMaterialaddressByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialaddressByName", startResult, maxRows, name);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@Transactional
	public Set<Materialaddress> findMaterialaddressByNameContaining(String name) throws DataAccessException {

		return findMaterialaddressByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Materialaddress> findMaterialaddressByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialaddressByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@Transactional
	public Set<Materialaddress> findMaterialaddressByDate(Calendar date) throws DataAccessException {

		return findMaterialaddressByDate(date, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Materialaddress> findMaterialaddressByDate(Calendar date, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialaddressByDate", startResult, maxRows, date);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@Transactional
	public Set<Materialaddress> findMaterialaddressByPrice(BigDecimal price) throws DataAccessException {

		return findMaterialaddressByPrice(price, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Materialaddress> findMaterialaddressByPrice(BigDecimal price, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialaddressByPrice", startResult, maxRows, price);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@Transactional
	public Set<Materialaddress> findMaterialaddressByDescription(String description) throws DataAccessException {

		return findMaterialaddressByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Materialaddress> findMaterialaddressByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialaddressByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@Transactional
	public Set<Materialaddress> findMaterialaddressByDescriptionContaining(String description)
			throws DataAccessException {

		return findMaterialaddressByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Materialaddress> findMaterialaddressByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMaterialaddressByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@Transactional
	public Materialaddress findMaterialaddressByPrimaryKey(Integer id) throws DataAccessException {
		return findMaterialaddressByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Materialaddress findMaterialaddressByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findMaterialaddressByPrimaryKey", startResult, maxRows, id);
			return (Materialaddress) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Materialaddress> findMaterialaddresssByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMaterialaddresssByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Materialaddress> findAllMaterialaddresss() throws DataAccessException {

		return findAllMaterialaddresss(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Materialaddress> findAllMaterialaddresss(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllMaterialaddresss", startResult, maxRows);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Materialaddress> findMaterialaddresssByAddress(Integer addressId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllMaterialaddresssByAddress", start, max, addressId);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Materialaddress> findMaterialaddresssByMaterial(Integer materialId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllMaterialaddresssByMaterial", start, max, materialId);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Materialaddress> findMaterialaddresssByUnit(Integer unitId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllMaterialaddresssByUnit", start, max, unitId);
		return new LinkedHashSet<Materialaddress>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Materialaddress> findMaterialaddressByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Materialaddress>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Materialaddress> findMaterialaddresssByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Materialaddress.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Materialaddress> materialaddresss = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return materialaddresss;
	}

	@Transactional
	public Materialaddress findMaterialaddressByPhysicalPrimaryKey(Integer materialId, Integer addressId, Calendar date)
			throws DataAccessException {
		return findMaterialaddressByPhysicalPrimaryKey(materialId, addressId, date, -1, -1);
	}

	@Transactional
	public Materialaddress findMaterialaddressByPhysicalPrimaryKey(Integer materialId, Integer addressId, Calendar date,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMaterialaddressByPhyscialPrimaryKey", startResult, maxRows, materialId,
					addressId, date);
			return (Materialaddress) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Materialaddress entity) {
		return true;
	}
}
